<template>
  <div class="cityAlpha">
    <div
      v-for="item of letters"
      :key="item"
      :ref="item"
      class="text-center text-info"
      @click="posAlpha(item)"
      @touchstart="handStart"
      @touchmove="handMove"
      @touchend="handEnd"
    >{{item}}</div>
  </div>
</template>

<script>
export default {
  name: "cityAlpha",
  props: {
    allCity: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      touchFlag: false,
      yPos: 0,
      timer: null
    };
  },
  updated() {
    this.yPos = this.$refs["A"][0].offsetTop;
  },
  components: {},
  methods: {
    posAlpha(k) {
      this.$emit("change", k);
    },
    handStart() {
      this.touchFlag = true;
    },
    handMove(e) {
      if (this.touchFlag) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          let tPos = e.touches[0].clientY - 76;
          let diff = Math.floor((tPos - this.yPos) / 24);
          if (diff >= 0 && diff < this.letters.length) {
            this.$emit("change", this.letters[diff]);
          }
        }, 16);
      }
    },
    handEnd() {
      this.touchFlag = false;
    }
  },
  mounted() {},
  computed: {
    letters() {
      const letters = [];
      for (let i in this.allCity) {
        letters.push(i);
      }
      return letters;
    }
  }
};
</script>


<style scoped>
.cityAlpha {
  position: absolute;
  top: 4.8rem;
  right: 0;
  bottom: 0;
  width: 1.2rem;
  background: rgba(255, 255, 255, 0.2);
}
</style>
